package com.liqw;

import com.liqw.core.LiveCourseConcurrentManager;
import com.liqw.core.DriverManager;
import com.liqw.core.LoginService;
import com.liqw.core.LiveCourseManager;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebDriver;

/**
 * 主类（协调登录、课程爬取流程）
 */
@Slf4j
public class OuchnApp {
    // 目标网站URL
    private static final String TARGET_URL = "https://student.syxy.ouchn.cn/";
    // 最大并发课程数
    private static final int MAX_CONCURRENT_COURSES = 3;

    public static void main(String[] args) {
        // 1. 初始化Driver（资源管理）
        DriverManager driverManager = new DriverManager();
        try {
            driverManager.initDriver();
            WebDriver driver = driverManager.getDriver();

            // 2. 执行登录
            LoginService loginService = new LoginService(driver, TARGET_URL);
            boolean loginSuccess = loginService.doLogin();
            if (!loginSuccess) {
                log.error("登录失败，无法继续爬取课程");
                return;
            }
            loginService.printUserInfo();

            // 选择使用并发观看还是单线程观看
            boolean useConcurrentMode = true; // 可以通过参数或配置文件控制

            if (useConcurrentMode) {
                // 使用并发模式观看直播课
                log.info("===== 开始并发观看直播课重播（最大 {} 个并发）=====", MAX_CONCURRENT_COURSES);
                LiveCourseConcurrentManager concurrentManager = new LiveCourseConcurrentManager(driver, MAX_CONCURRENT_COURSES);
                concurrentManager.playRecordedLiveCoursesConcurrently();
            } else {
                // 原有方式观看直播课
                log.info("===== 开始观看直播课重播 =====");
                LiveCourseManager liveCourseManager = new LiveCourseManager(driver);
                liveCourseManager.playRecordedLiveCourses();
            }

        } catch (Exception e) {
            log.error("程序执行异常", e);
        } finally {
            // 4. 释放资源
            driverManager.quitDriver();
            log.info("程序执行完成，已释放所有资源");
        }
    }
}